﻿using ADODB;
using fuzhu;
using LH.Model.PU;
using LH.Model.ST;
using SqlSuarEntity;
using SqlSugar;
using System;
using System.Data;
using System.Security.Cryptography;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WMS;
using XXX.Models.XXXEntities;
using ZDEntity;

namespace Yonyou.U8.BO
{

    public class WorkListVM 
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";

        public WorkListVM()  
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }
        //清除敏感信息
        public void ClearConstr()
        {
            conStr = null;
            //初始化定义xmlconfig
            xmlConfig =null;
        }

        /// <summary>
        /// 查询待扫描的信息，根据ctype进行区分
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel2 GetEntityList(string cType,string cwhcode, string ccode)
        {
            //采购到货列表，用于扫描采购入库
            
            switch (cType)
            {
                case "puarrivallist":
                    return GetPuArrivalList(cwhcode, ccode,false);
                case "puarrivallistRed":
                    return GetPuArrivalList(cwhcode, ccode,true);
                case "dispatchlist":
                    return GetDispatchList(cwhcode, ccode,0);
                case "dispatchlistred":
                    return GetDispatchList(cwhcode, ccode, 1);
                case "transvouchapplist":
                    return GetTransvouchAppList(cwhcode, ccode);
                case "materialapplist":
                    return GetMaterialAppList(cwhcode, ccode);
                case "molist":
                    return GetMoList(cwhcode, ccode);
                case "mesrklist":
                    //mes待入库清单
                    return GetMesRKList(cwhcode, ccode);
                // 可以继续添加更多的 case
                //case "anotherType":
                //    return GetAnotherFunction(ccode);
                default:
                    // 默认情况下的处理
                    Console.WriteLine("未知的类型:", cType);
                    return DBService.ReErrObject("未定义列表");
            }
            return DBService.ReErrObject("未定义列表");
            

        }

        public RetModel2 GetEntity(string cType,  string ccode)
        {
           
            switch (cType)
            {
                //采购到货列表，用于扫描采购入库
                case "puarrivallist":
                    return GetPuArrivalEntity(ccode,false);
                case "puarrivallistRed":
                    return GetPuArrivalEntity(ccode,true);
                case "dispatchlist":
                    return GetDispatchEntity(ccode);
                case "transvouchapplist":
                    return GetTransvouchEntity(ccode);
                case "materialapplist":
                    return GetMaterialAppEntity(ccode);
                case "molist":
                    return GetMoEntity(ccode);
                case "mesrklist":
                    return GetMesRKEntity(ccode);
                // 可以继续添加更多的 case
                //case "anotherType":
                //    return GetAnotherFunction(ccode);
                default:
                    // 默认情况下的处理
                    Console.WriteLine("未知的类型:", cType);
                    return DBService.ReErrObject("未定义列表");
            }
         


        }
        /// <summary>
        /// 获取到货单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetPuArrivalList(string cwhcode, string ccode,bool bred)
        {
            try
            {

                RetModel retModel = new RetModel();
                string conditionsql = "";
                if (ccode != "0")
                {
                    conditionsql += string.Format(" and a.cCode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    conditionsql += string.Format(" and  isnull(b.cwhcode,'{0}') = '{0}'   ", cwhcode);
                }
                //数量换算的系数
                int iXs = 1;
                if (bred)
                {
                    conditionsql += " and  b.iQuantity<0";
                    iXs = -1;
                }
                else
                {
                    conditionsql += " and  b.iQuantity>0";
                }
                string sql = string.Format(@"SELECT '采购到货单' cType,  a.cCode,a.cVenCode as cDWCode,a.cDepCode,a.cPersonCode,a.cPtCode,a.dDate,c.cVenName  as cDWName, 
sum(b.iQuantity-(ISNULL(b.fValidInQuan,0)+ISNULL(b.fRefuseQuantity,0)))*{2} sumwrksl ,
b.cWhCode, sum(b.iQuantity)*{2} as iQuantity,sum(ISNULL(b.fValidInQuan,0))*{2} as fValidInQuan 
FROM dbo.PU_ArrivalVouch a
INNER JOIN dbo.PU_ArrivalVouchs b ON  a.id = b.ID
INNER JOIN vendor c ON a.cVenCode=c.cvencode  
WHERE 1=1    {0} and ISNULL(b.cbcloser,'')='' and b.bgsp=0   AND ISNULL(a.cverifier,'')<>''
AND b.iQuantity*{2}>(ISNULL(b.fValidInQuan,0)*{2}+ISNULL(b.fRefuseQuantity,0)*{2}) 
and not exists(select 1 from inventory i where b.cinvcode=i.cinvcode and i.bService=1)
and a.ddate>='{1}'
group by a.cCode,a.cVenCode,a.cDepCode,a.cPersonCode,a.cPtCode,a.dDate,c.cVenName,b.cWhCode
order by a.cCode 
", conditionsql,  CanShu.dBegin, iXs);

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    { 
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未入库的到货单");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }



        /// <summary>
        /// 获取到货单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetPuArrivalEntity(string ccode, bool bred)
        {
            try
            {

                RetModel retModel = new RetModel();
                //数量换算的系数
             
                int iXs = 1;
                if (bred)
                {
                    
                    iXs = -1;
                }
              
                string sql = string.Format(@"SELECT a.cCode,a.cVenCode as cDWCode,a.cDepCode,a.cPersonCode,a.cPtCode,a.dDate,c.cVenName as cDWName,b.cInvCode,d.cInvName,com.cComUnitName, 
b.iQuantity*{1}-(ISNULL(b.fValidInQuan,0)*{1}+ISNULL(b.fRefuseQuantity,0)*{1}) wrksl,b.autoid,
b.cWhCode,a.id,b.iQuantity*{1},(ISNULL(b.fValidInQuan,0))*{1} as fValidInQuan,b.ivouchrowno as iRowNo,0.00 as scanqty
FROM dbo.PU_ArrivalVouch a
INNER JOIN dbo.PU_ArrivalVouchs b ON  a.id = b.ID
INNER JOIN vendor c ON a.cVenCode=c.cvencode
INNER JOIN inventory d ON b.cinvcode=d.cinvcode
INNER JOIN dbo.ComputationUnit com ON d.cComUnitCode=com.cComunitCode
WHERE 1=1    and a.cCode = '{0}' and ISNULL(b.cbcloser,'')='' and b.bgsp=0   AND ISNULL(a.cverifier,'')<>''
AND b.iQuantity*{1}>(ISNULL(b.fValidInQuan,0)*{1}+ISNULL(b.fRefuseQuantity,0)*{1}) 
and d.bService=0   order by a.cCode, b.cInvCode", ccode, iXs);
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);
                   
                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        string cErr = "";
                        if (bred)
                        {
                            cErr = "当前退货单已全部出库";
                        }
                        else
                        {
                            cErr = "当前到货单已全部入库";

                        }
                        return DBService.ReErrObject(cErr);
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        /// 获取到货单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <param name="bRed">是否红字，0，不是。1 是</param>
        /// <returns></returns>
        public RetModel2 GetDispatchList(string cwhcode, string ccode,int bRed)
        {
            try
            {

                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT '销售发货单' cType,  a.cDLCode as  cCode,a.cCusCode as cDWCode,a.cDepCode,a.cPersonCode,a.cSTCode,a.dDate,c.cCusName  as cDWName, 
sum(b.iQuantity-(ISNULL(b.fOutQuantity,0))) sumwrksl ,
b.cWhCode, sum(b.iQuantity) as iQuantity,sum(ISNULL(b.fOutQuantity,0)) as fValidInQuan 
FROM dbo.dispatchlist a
INNER JOIN dbo.dispatchlists b ON  a.dlid = b.dlid
INNER JOIN customer c ON a.cCusCode=c.cCusCode  
WHERE 1=1    ");
                if (ccode != "0")
                {
                    sql += string.Format(" and a.cDLCode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    sql += string.Format(" and  isnull(b.cwhcode,'{0}') = '{0}'   ", cwhcode);
                }
                //是否红字
                if (bRed==1)
                {
                    sql += string.Format(" and    b.iQuantity<0  ");
                }
                else
                {
                    sql += string.Format(" and    b.iQuantity>0  ");
                }
                //排序
                sql += String.Format(@"  and ISNULL(b.cSCloser,'')='' and b.bgsp=0   AND ISNULL(a.cverifier,'')<>''
AND b.iQuantity>(ISNULL(b.fOutQuantity,0) ) 
and not exists(select 1 from inventory i where b.cinvcode=i.cinvcode and i.bService=1)
and a.ddate>='{0}'
group by a.cDLCode,a.cCusCode,a.cDepCode,a.cPersonCode,a.cSTCode,a.dDate,c.cCusName,b.cWhCode
order by a.cDLCode 
", CanShu.dBegin);

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未出库的发货单");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        /// 获取到货单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetDispatchEntity(string ccode)
        {
            try
            {

                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT a.cDLCode AS cCode,a.cCusCode as cDWCode,a.cDepCode,a.cPersonCode,a.cSTCode,a.dDate,c.cCusName as cDWName,b.cInvCode,d.cInvName,com.cComUnitName, 
b.iQuantity-(ISNULL(b.fOutQuantity,0) ) wrksl,b.idlsid as  autoid,
b.cWhCode,a.dlid as id,b.iQuantity,(ISNULL(b.fOutQuantity,0)) as fValidInQuan,b.irowno as iRowNo,0.00 as scanqty
FROM dbo.DispatchList a
INNER JOIN dbo.DispatchLists b ON  a.DLID = b.DLID
INNER JOIN Customer c ON a.cCusCode=c.cCusCode
INNER JOIN inventory d ON b.cinvcode=d.cinvcode
INNER JOIN dbo.ComputationUnit com ON d.cComUnitCode=com.cComunitCode
WHERE 1=1     and a.cDLCode = '{0}' and ISNULL(b.cSCloser,'')='' and b.bgsp=0   AND ISNULL(a.cverifier,'')<>''
AND b.iQuantity>(ISNULL(b.fOutQuantity,0) ) 
and not exists(select 1 from inventory i where b.cinvcode=i.cinvcode and i.bService=1)
  order by a.cDLCode, b.cInvCode", ccode);
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);
                    foreach (WorkList pd in pdlist)
                    {
                        pd.UseDefaultPosCode =WMSPara.UseDefaultPosCode;
                        pd.StatusControl = WMSPara.StatusControl;
                    }


                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("当前到货单已全部入库");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }


        /// <summary>
        /// 获取调拨申请单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetTransvouchAppList(string cwhcode, string ccode)
        {
            try
            {

                RetModel retModel = new RetModel();
                string strWhere = "";
                if (ccode != "0")
                {
                    strWhere += string.Format(" and a.cTVCode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    strWhere += string.Format(" and  (a.cOWhCode = '{0}' ) ", cwhcode);
                }
                string sql =string.Format(@"SELECT '调拨申请单' cType,  a.cTVCode  as cCode, a.cODepCode as cDepCode,a.cPersonCode,  a.dTVDate AS  dDate,  
sum(b.iTvChkQuantity-(ISNULL(b.iTvSumQuantity,0) )) sumwrksl ,
a.cOWhCode as cWhCode, a.cIWhCode as cWhCodeIn,sum(b.iTvChkQuantity) as iQuantity,sum(ISNULL(b.iTvSumQuantity,0)) as fValidInQuan 
FROM dbo.ST_AppTransVouch a
INNER JOIN dbo.ST_AppTransVouchs b ON  a.id = b.ID 
WHERE 1=1   {0}   and ISNULL(b.cbcloser,'')=''  AND ISNULL(a.cVerifyPerson,'')<>''
AND b.iTvChkQuantity>(ISNULL(b.iTvSumQuantity,0) )  
and a.dTVDate>='{1}'
group by a.cTVCode, a.cODepCode,a.cPersonCode, a.dTVDate, a.cOWhCode,a.cIWhCode
order by a.cTVCode 
", strWhere, CanShu.dBegin);

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);
                    //出库参数带上
                    foreach (WorkList pd in pdlist)
                    {
                        pd.UseDefaultPosCode = WMSPara.UseDefaultPosCode;
                    }
                    if (pdlist.Count > 0)
                    {
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未完成的调拨申请单");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }


        /// <summary>
        /// 获取调拨申请单
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetTransvouchEntity(string ccode)
        {
            try
            {
                //仓库是否有货位，带上
                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT a.cTVCode AS cCode, a.cODepCode,a.cPersonCode, a.dTVDate AS dDate, b.cInvCode,d.cInvName,com.cComUnitName, 
b.iTvChkQuantity-(ISNULL(b.iTvSumQuantity,0) ) wrksl,b.autoID as  autoid,
a.cOWhCode as cWhCode , a.cIWhCode as cWhCodeIn,a.ID as id,b.iTvChkQuantity as iQuantity,(ISNULL(b.iTvSumQuantity,0)) as fValidInQuan,b.irowno as iRowNo,0.00 as scanqty
,convert(varchar(20), wo.bWhPos) as bWhPos ,convert(varchar(20), wi.bWhPos)  as bWhInPos,wo.cWhName,wi.cWhName as cWhNameIn
FROM dbo.ST_AppTransVouch a
INNER JOIN dbo.ST_AppTransVouchs b ON   a.id = b.id 
INNER JOIN inventory d ON b.cinvcode=d.cinvcode
INNER JOIN dbo.ComputationUnit com ON d.cComUnitCode=com.cComunitCode
inner join Warehouse wo on a.cOWhCode =wo.cWhCode
inner join Warehouse wi on a.cIWhCode = wi.cWhCode
WHERE 1=1   and a.cTVCode = '{0}'  and ISNULL(b.cBCloser,'')=''   AND ISNULL(a.cVerifyPerson,'')<>''
AND b.iTvChkQuantity>(ISNULL(b.iTvSumQuantity,0) )  
   order by a.cTVCode, b.cInvCode", ccode );
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("当前到货单已全部入库");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        /// 获取领料申请单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMaterialAppList(string cwhcode, string ccode)
        {
            try
            {

                string cSqlWhere = "";
                if (ccode != "0")
                {
                    cSqlWhere += string.Format(" and a.cCode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    cSqlWhere += string.Format(" and  b.cwhcode = '{0}'   ", cwhcode);
                }
                string sql = string.Format(@"SELECT '领料申请单' cType,  a.cCode,a.cVenCode as cDWCode,a.cDepCode as cDWCode,a.cPersonCode, a.dDate, dep.cDepName as cDWName,
sum(b.iQuantity-(ISNULL(b.fOutQuantity,0))) sumwrksl ,
b.cWhCode, sum(b.iQuantity) as iQuantity,sum(ISNULL(b.fOutQuantity,0)) as fValidInQuan 
FROM dbo.MaterialAppVouch a
INNER JOIN dbo.MaterialAppVouchs b ON  a.id = b.ID 
left join Department dep on a.cDepCode = dep.cDepCode
WHERE 1=1   {0}  and ISNULL(b.cbcloser,'')=''    AND ISNULL(a.cHandler,'')<>''
AND b.iQuantity>(ISNULL(b.fOutQuantity,0) )  
and a.ddate>='{1}'
group by a.cCode,a.cVenCode,a.cDepCode,a.cPersonCode, a.dDate,dep.cDepName,b.cWhCode
order by a.cCode 
", cSqlWhere, CanShu.dBegin);

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未出库的领料申请单");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }



        /// <summary>
        /// 获取到货单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMaterialAppEntity(string ccode)
        {
            try
            {

                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT a.cCode,a.cVenCode as cDWCode,a.cDepCode as cDWCode, dep.cDepName as cDWName,a.cPersonCode, a.dDate,  b.cInvCode,d.cInvName,com.cComUnitName, b.iQuantity-(ISNULL(b.fOutQuantity,0) ) wrksl,b.autoid,
b.cWhCode,a.id,b.iQuantity,(ISNULL(b.fOutQuantity,0)) as fValidInQuan,b.irowno as iRowNo,0.00 as scanqty
FROM dbo.MaterialAppVouch a
INNER JOIN dbo.MaterialAppVouchs b ON  a.id = b.ID 
INNER JOIN inventory d ON b.cinvcode=d.cinvcode
left join Department dep on a.cDepCode = dep.cDepCode
left JOIN dbo.ComputationUnit com ON d.cComUnitCode=com.cComunitCode
WHERE 1=1    and a.cCode = '{0}'    and ISNULL(b.cbcloser,'')=''  AND ISNULL(a.cHandler,'')<>''
AND b.iQuantity>(ISNULL(b.fOutQuantity,0) ) 
and d.bService=0   order by a.cCode, b.cInvCode", ccode);
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("当前领料申请已全部出库");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        /// 获取生产订单列表
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMoList(string cwhcode, string ccode)
        {
            try
            {

                string cSqlWhere = "";
                if (ccode != "0")
                {
                    cSqlWhere += string.Format(" and a.mocode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    cSqlWhere += string.Format(" and  isnull(b.WhCode,'{0}') = '{0}'   ", cwhcode);
                }
                string sql = string.Format(@"SELECT '生产订单' cType,  a.mocode as cCode, b.mDeptCode as cDWCode,   dep.cDepName as cDWName,
sum(b.Qty-(ISNULL(b.QualifiedInQty,0))) sumwrksl ,
b.WhCode as cWhCode, sum(b.Qty) as iQuantity,sum(ISNULL(b.QualifiedInQty,0)) as fValidInQuan 
FROM dbo.mom_order a
INNER JOIN dbo.mom_orderdetail b ON  a.moid = b.MoId 
INNER JOIN dbo.mom_morder c ON  b.modid = c.MoDId 

left join Department dep on b.mDeptCode = dep.cDepCode
WHERE 1=1   {0} and b.Status=3    
AND b.Qty>(ISNULL(b.QualifiedInQty,0) )  
and c.StartDate>='{1}'
group by a.mocode,b.mDeptCode,dep.cDepName,b.WhCode   
order by a.mocode 
", cSqlWhere, CanShu.dBegin);

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未入库的生产订单");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }


        /// <summary>
        /// 获取mes系统推送的入库申请单
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMesRKList(string cwhcode, string ccode)
        {
            try
            {

                string cSqlWhere = "";
                if (ccode != "0")
                {
                    cSqlWhere += string.Format(" and a.cSourceCode = '{0}'", ccode);
                }
                if (!string.IsNullOrWhiteSpace(cwhcode))
                {
                    //仓库相同或者仓库是空的
                    cSqlWhere += string.Format(" and  isnull(a.cWhCode,'{0}') = '{0}'   ", cwhcode);
                }
                string sql = string.Format(@"SELECT '生产订单' cType,  a.cSourceCode as cCode, ms.MDeptCode as cDWCode,  b.cDepName as  cDWName,
sum(a.iQuantity) sumwrksl ,
a.cWhCode as cWhCode, sum(a.iQuantity) as iQuantity,0 as fValidInQuan 
FROM ZDY_RuKuApp a
inner join mom_orderdetail ms on  a.iSourceId = ms.MoDId
left join Department b on ms.MDeptCode = b.cDepCode 
WHERE 1=1 {0}  
group by a.cSourceCode ,a.cWhCode , ms.MDeptCode,  b.cDepName
", cSqlWhere );

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = db.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("没有未入库的入库申请单");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }
        /// <summary>
        /// 获取生产订单 
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMoEntity(string ccode)
        {
            try
            {

                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT  a.mocode as cCode, b.mDeptCode as cDWCode,   dep.cDepName as cDWName,    b.InvCode as  cInvCode,d.cInvName,com.cComUnitName, 
b.Qty-(ISNULL(b.QualifiedInQty,0) ) wrksl,b.MoDId as autoid,
b.WhCode as  cWhCode,a.moid as id,b.qty as iQuantity,(ISNULL(b.QualifiedInQty,0)) as fValidInQuan,b.SortSeq as iRowNo,0.00 as scanqty
FROM dbo.mom_order a
INNER JOIN dbo.mom_orderdetail b ON  a.moid = b.MoId 
INNER JOIN dbo.mom_morder c ON  b.modid = c.MoDId 

INNER JOIN inventory d ON b.invcode=d.cinvcode
inner join Department dep on b.mDeptCode = dep.cDepCode
INNER JOIN dbo.ComputationUnit com ON d.cComUnitCode=com.cComunitCode
WHERE 1=1    and a.mocode = '{0}'    and b.Status=3
AND b.qty>(ISNULL(b.QualifiedInQty,0) ) 
and d.bService=0   order by a.mocode, b.InvCode", ccode);
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("当前领料申请已全部出库");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }


        /// <summary>
        /// 获取MES入库申请的实体 
        /// </summary>
        /// <param name="cwhcode"></param>
        /// <param name="ccode"></param>
        /// <returns></returns>
        public RetModel2 GetMesRKEntity(string ccode)
        {
            try
            {

                RetModel retModel = new RetModel();
                string sql = string.Format(@"SELECT  a.cSourceCode as cCode, ms.MDeptCode as cDWCode,  b.cDepName as cDWName,    a.cInvCode as  cInvCode,i.cInvName,com.cComUnitName, 
a.iQuantity wrksl,ms.MoDId as autoid,
a.cWhCode as  cWhCode,ms.moid as id,a.iQuantity as iQuantity,0 as fValidInQuan,1 as iRowNo,0.00 as scanqty
FROM ZDY_RuKuApp a
inner join mom_orderdetail ms on  a.iSourceId = ms.MoDId
left join Department b on ms.MDeptCode = b.cDepCode 
left join Inventory i on a.cInvCode =i.cInvCode
left join ComputationUnit com on i.cComUnitCode =com.cComunitCode
WHERE a.cSourceCode = '{0}'   
and not exists(select 1 from rdrecord10 rd where  a.parentid = rd.cdefine10)", ccode);
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    List<WorkList> pdlist = SqlsugarHelper._Instance.Ado.SqlQuery<WorkList>(sql);

                    if (pdlist.Count > 0)
                    {
                        var dSum = pdlist.Sum(t => t.wrksl);
                        pdlist[0].sumwrksl = dSum;
                        return DBService.ReOKObject(pdlist);
                    }
                    else
                    {
                        return DBService.ReErrObject("当前已入库");
                    }
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }
    }
}
